package com.yupi.springbootinit.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.springbootinit.common.BaseResponse;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.common.ResultUtils;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.springbootinit.model.dto.course.CourseAddRequest;
import com.yupi.springbootinit.model.dto.course.CourseQueryRequest;
import com.yupi.springbootinit.model.entity.Course;
import com.yupi.springbootinit.model.entity.User;
import com.yupi.springbootinit.model.vo.ChapterVO;
import com.yupi.springbootinit.model.vo.CourseVO;
import com.yupi.springbootinit.service.ChapterService;
import com.yupi.springbootinit.service.CourseService;
import com.yupi.springbootinit.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 课程控制器
 */
@RestController
@RequestMapping("/course")
@Slf4j
public class CourseController {

    @Resource
    private CourseService courseService;
    
    @Resource
    private UserService userService;
    
    @Resource
    private ChapterService chapterService;

    /**
     * 获取课程列表
     */
    @GetMapping("/list/page")
    public BaseResponse<Page<Course>> listCourseByPage(CourseQueryRequest courseQueryRequest) {
        if (courseQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Page<Course> coursePage = courseService.listCourses(courseQueryRequest);
        return ResultUtils.success(coursePage);
    }

    /**
     * 根据 id 获取课程详情
     */
    @GetMapping("/get")
    public BaseResponse<CourseVO> getCourseById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        CourseVO courseVO = courseService.getCourseById(id, request);
        return ResultUtils.success(courseVO);
    }

    /**
     * 收藏/取消收藏课程（需要登录）
     */
    @PostMapping("/favorite")
    public BaseResponse<Integer> favoriteCourse(@RequestBody CourseAddRequest courseAddRequest,
            HttpServletRequest request) {
        if (courseAddRequest == null || courseAddRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 登录才能收藏
        User loginUser = userService.getLoginUser(request);
        long courseId = courseAddRequest.getId();
        int result = courseService.favoriteCourse(courseId, loginUser);
        return ResultUtils.success(result);
    }
    
    /**
     * 报名课程（需要登录）
     */
    @PostMapping("/enroll")
    public BaseResponse<Boolean> enrollCourse(@RequestBody CourseAddRequest courseAddRequest,
            HttpServletRequest request) {
        if (courseAddRequest == null || courseAddRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 登录才能报名
        User loginUser = userService.getLoginUser(request);
        long courseId = courseAddRequest.getId();
        boolean result = courseService.enrollCourse(courseId, loginUser);
        return ResultUtils.success(result);
    }

    /**
     * 获取章节详情
     */
    @GetMapping("/chapter")
    public BaseResponse<ChapterVO> getChapterById(long id, long chapter, HttpServletRequest request) {
        if (id <= 0 || chapter <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        
        // 获取登录用户（允许未登录）
        User loginUser = userService.getLoginUserPermitNull(request);
        
        // 调用 service 层获取章节详情
        ChapterVO chapterVO = chapterService.getChapterDetail(id, chapter, loginUser);
        return ResultUtils.success(chapterVO);
    }
} 